import 'package:flutter/material.dart';
import 'package:flutter_app/global/color_config.dart';
import 'package:flutter_app/global/flutter_toast.dart';
import 'package:flutter_app/icon/constant_icons.dart';
import 'package:flutter_app/main/main/home/home_table.dart';
import 'package:flutter_app/main/main/main_drawer.dart';
import 'package:flutter_app/main/main/official_accounts/account.dart';
import 'package:flutter_app/main/main/project/project.dart';
import 'package:flutter_app/main/main/system/system.dart';

class MainChildWidget extends StatefulWidget {
  const MainChildWidget({super.key});

  @override
  MainChildPage createState() => MainChildPage();
}

class MainChildPage extends State<MainChildWidget> {
  String titleBar = '玩Android';
  int index = 0;

  final List<BottomNavigationBarItem> bottomNavigationItems = [
    const BottomNavigationBarItem(
        icon: Icon(CustantIcons.home), label: '首页', tooltip: '首页'),
    const BottomNavigationBarItem(
        icon: Icon(CustantIcons.account), label: '公众号', tooltip: '公众号'),
    const BottomNavigationBarItem(
        icon: Icon(CustantIcons.system), label: '体系', tooltip: '体系'),
    const BottomNavigationBarItem(
        icon: Icon(CustantIcons.project), label: '项目', tooltip: '项目')
  ];

  final page = [
    const HomeTableWidget(),
    const AccountWidget(),
    const SystemWidget(),
    const ProjectWidget()
  ];

  void changePage(currentIndex) {
    setState(() {
      index = currentIndex;
      if (index == 0) {
        titleBar = '玩Android';
      } else if (index == 1) {
        titleBar = '公众号';
      } else if (index == 2) {
        titleBar = '体系';
      } else if (index == 3) {
        titleBar = '项目';
      }
    });
  }

  @override
  void initState() {
    super.initState();
    index = 0;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: const MainDrawerWidget(),
      appBar: AppBar(
        elevation: 0,
        leading: Builder(builder: (BuildContext con) {
          return IconButton(
              onPressed: () {
                Scaffold.of(con).openDrawer();
              },
              icon: const Icon(
                Icons.dehaze,
                color: Colors.white,
              ));
        }),
        title: Text(
          titleBar,
          style: const TextStyle(fontSize: 18, color: Colors.white),
        ),
        actions: <Widget>[
          IconButton(
            icon: const Icon(
              Icons.search,
              color: Colors.white,
            ),
            color: Colors.white,
            onPressed: () {
              FlutterToast.showToast('跳转到搜索页');
              //跳转到搜索页
              // Navigator.push(
              //     context,
              //     MaterialPageRoute(
              //         builder: (context) => const ShareArticleWidget()));
            },
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: bottomNavigationItems,
        currentIndex: index,
        selectedItemColor: ColorConfig.themeColor,
        unselectedItemColor: Colors.black38,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          changePage(index);
        },
      ),
      body: IndexedStack(
        index: index,
        children: page,
      ),
    );
  }
}
